local width = 32;
local height = 32;

local lastSeconds = 0;
local lastHp = 0;
local timeToDie = 0;
local currentSeconds = 0;

local health = 0;
local max_health = 0;
local is_dead = false;
local name = "";

local dps = 0;
local dmg_count = 0;
local dmg_total = 0;

local ESTIM = 0;
local AVERAGE_ESTIM = 1;
local calc = ESTIM;

local UpdateInterval = 1.0;
local TimeSinceLastUpdate = 0;

local html = "";
local fontSize = 11;

local x = 0;
local y = 0;

local frame = CreateFrame('SimpleHTML');
frame:SetPoint("CENTER", 0, 0);
frame:EnableMouse(true);
frame:SetMovable(true);
frame:SetWidth(width);
frame:SetHeight(height);

frame:RegisterEvent("PLAYER_TARGET_CHANGED");
frame:RegisterEvent("UNIT_HEALTH");
frame:RegisterEvent("UNIT_COMBAT");
-- Fired whenever the level of a unit is submitted (e.g. when clicking a unit or someone joins the party) 
frame:RegisterEvent("UNIT_LEVEL");
-- Fired when a units stats are being passed to the player/thing 
frame:RegisterEvent("UNIT_STATS");

-- Can happen on some bosses or mobs
frame:RegisterEvent("UNIT_MAXHEALTH");
frame:RegisterEvent("UNIT_MAXPOWER");
frame:RegisterEvent("UNIT_MODEL_CHANGED");
frame:RegisterEvent("UNIT_NAME_UPDATE");

frame:RegisterEvent("ADDON_LOADED"); -- Fired when saved variables are loaded
frame:RegisterEvent("PLAYER_LOGOUT"); -- Fired when about to log out

frame:SetFont('Fonts\\FRIZQT__.TTF', fontSize);
frame:SetScript("OnMouseDown", function(self, button)
  if button == "LeftButton" and not self.isMoving then
   self:StartMoving();
   self.isMoving = true;
  end
end)
frame:SetScript("OnMouseUp", function(self, button)
  if button == "LeftButton" and self.isMoving then
   self:StopMovingOrSizing();
   self.isMoving = false;
  end
end)
frame:SetScript("OnHide", function(self)
  if ( self.isMoving ) then
   self:StopMovingOrSizing();
   self.isMoving = false;
  end
end)

local function eventHandler(self, event, ...)	
	if event == "PLAYER_TARGET_CHANGED" then
		ResetData();
	end
	
	if event == "ADDON_LOADED" and arg1 == "DeathTimerX" then
		x = frame:GetLeft();
		y = frame:GetBottom();
	end
	Update();
end

frame:SetScript("OnEvent", eventHandler);

function Update()
	GetTargetData();
	UpdateDPS_Time();
	
	if name ~= nil then
		lastSeconds = time();
		lastHp = UnitHealth("target");
	end
	
	UpdateText();
end

function ResetData()
	name = nil;
	timeToDie = 0;
	dps = 0;
	dmg_total = 0;
	dmg_count = 0;
end

function GetTargetData()
	max_health = UnitHealthMax("target");
	name = GetUnitName("target");
end

function UpdateDPS_Time()
	currentSeconds = time();
	health = UnitHealth("target");
	dps = (lastHp-health)*(currentSeconds-lastSeconds);

	if dps > 0 then
		dmg_total = dmg_total + dps;
		dmg_count = dmg_count + 1;
		
		if calc == ESTIM then
			--Estimated calculation
			timeToDie = health/dps;
		elseif calc == AVERAGE_ESTIM then
			-- Average estimated calculation
			timeToDie = health/(dmg_total/dmg_count);	
		end
	end	
	lastHp = health;
	lastSeconds = currentSeconds;
end

function UpdateText()
              wasNil = false;
	if name == nil then
                            wasNil = true;
                            name = "No Target";
                            timeToDie = 0;
	else
		html = 
                            "<html>".. 
                              "<body>" .. 
                                "<p>" .. 
                                  name .. 
                                  "<br/>" .. 
                                  date("%M:%S", timeToDie) .. 
                                "</p>".. 
                             "</body>".. 
                           "</html>";
		frame:SetWidth((string.len(name) * fontSize) * 0.9);
	              if wasNil then 
                                  name = nil;
                            end
              end
	frame:SetText(html);
end